<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS;

class VerifySign
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $params = $request->all();

        // 重置赋值
        $verifyStr = $params['verifyStr'];
        unset($params['verifyStr']);

        // 参数排序
        ksort($params);
        $str = '';

        // 拼接字符串数据
        foreach ($params as $k =>$v)
        {
            $str .= $k.$v;
        }

        $sign = 'imAPIZW2YaajD0RsIUUf9';
        $signStr = md5($str.$sign);

        if($signStr != $verifyStr)
        {
            return errBack('签名失败');
        }

        return $next($request);
    }
}
